﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data;
using DevExpress.XtraEditors.Controls;

namespace SoufunData
{
    public partial class ZYuShouZhengForm : DevExpress.XtraEditors.XtraForm
    {

        public ZYuShouZhengForm()
        {
            InitializeComponent();
            dateEdit_start.DateTime = DateTime.Now.AddDays(-60);
            dateEdit_end.DateTime = DateTime.Now;
            _dtResult = new DataTable();
            _dtResult.Columns.Add("预售许可证号");
            _dtResult.Columns.Add("项目名称");
            _dtResult.Columns.Add("核准预售总套数");
            _dtResult.Columns.Add("核准预售总面积");
            _dtResult.Columns.Add("分户型预售套数");
            _dtResult.Columns.Add("分户型预售面积");
            
            DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
        }
         
        private DataTable _dtResult;
        private readonly SqlClass _sqlClass = new SqlClass();

        private void AdvertisingForm_Load(object sender, EventArgs e)
        { 
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }

        
         
        private void LoatData()
        {
            

            StringBuilder strWhere = new StringBuilder();

            //strWhere.Append("PublicTime between '");
            //strWhere.Append(dateEdit_start.DateTime);
            //strWhere.Append("' and '");
            //strWhere.Append(dateEdit_end.DateTime);
            //strWhere.Append("' ");
             
            //if (imgCombox_xzq.SelectedIndex!=0)
            //{
            //    strWhere.Append(" and ");
            //    strWhere.Append("AdministratiVeregion='");
            //    strWhere.Append(imgCombox_xzq.Properties.Items[
            //        imgCombox_xzq.SelectedIndex].Description);
            //    strWhere.Append("' ");
            //}
            //if (imgCombox_wy.SelectedIndex!=0)
            //{
            //    if (strWhere.ToString() != "")
            //    {
            //        strWhere.Append(" and ");
            //    }
            //    strWhere.Append("PropertyType='");
            //    strWhere.Append(imgCombox_wy.Properties.Items[
            //        imgCombox_wy.SelectedIndex].Description);
            //    strWhere.Append("' ");
            //}
            //if (imgCombox_media.SelectedIndex!=0)
            //{
            //    if (strWhere.ToString() != "")
            //    {
            //        strWhere.Append(" and ");
            //    }
            //    strWhere.Append(" PublicMedia='");
            //    strWhere.Append(imgCombox_media.Properties.Items[
            //        imgCombox_media.SelectedIndex].Description);
            //    strWhere.Append("' ");
            //}
             
            //if (textEdit_proName.Text.Trim()!="")
            //{
            //    if (strWhere.ToString()!="")
            //    {
            //        strWhere.Append(" and ");
            //    }
            //    strWhere.Append("ProjectName like '%");
            //    strWhere.Append(textEdit_proName.Text);
            //    strWhere.Append("%'");
            //}


            try
            {
                DataSet ds = _sqlClass.ZYushouzhengGetList(strWhere.ToString());
                if (ds == null || ds.Tables.Count == 0)
                {
                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                    return;
                }
                GetResultData(ds.Tables[0]);

                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl2.DataSource = ds.Tables[0];
                    gridView2.GroupPanelText = "共记"
                        + ds.Tables[0].Rows.Count + "条数据";
                }
                ));
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            } 
        }

        private void GetResultData(DataTable dt)
        {
            _dtResult.Clear();
            foreach (DataRow row in dt.Rows)
            {
                string proName = row["ProjectName"].ToString();
                string strNumber = row["Number"].ToString();
                int presellcount = int.Parse(row["PreSellCount"].ToString());             
                float presellare = float.Parse(row["PreSellArea"].ToString());
                int houseprecount = int.Parse(row["HousePreSellCount"].ToString());
                float housepreArea = float.Parse(row["HousePreSellArea"].ToString());

                DataRow[] drs = _dtResult.Select("项目名称='" 
                    + proName+"'");
                if (drs.Length==0)
                {
                    DataRow newRow = _dtResult.NewRow();
                    newRow["项目名称"] = proName;
                    newRow["预售许可证号"] = strNumber;
                    newRow["核准预售总套数"] = presellcount;
                    newRow["核准预售总面积"] = presellare;
                    newRow["分户型预售套数"] = houseprecount;
                    newRow["分户型预售面积"] = housepreArea;
                     
                    _dtResult.Rows.Add(newRow);
                }
                else
                {
                    drs[0]["核准预售总套数"] = int.Parse(drs[0]["核准预售总套数"].ToString())
                        + presellcount;
                    drs[0]["核准预售总面积"] = float.Parse(drs[0]["核准预售总面积"].ToString())
                        + presellare;
                    drs[0]["分户型预售套数"] = int.Parse(drs[0]["分户型预售套数"].ToString())
                        + houseprecount;
                    drs[0]["分户型预售面积"] = float.Parse(drs[0]["分户型预售面积"].ToString())
                        + housepreArea;
                }
            }
            BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl1.DataSource = _dtResult;
                    gridView1.GroupPanelText = "共计" + _dtResult.Rows.Count + "条数据";
                }));
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            gridView2.GroupPanelText = "正在查询...";
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
         
        private void textEdit_proName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
               btn_select_Click(null,null);
            }
        }
         
        private void btn_toExcel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "广告投放数据";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView2.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }
 

        private void btn_importResultExcel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "广告投放统计";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }
    }
}